﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.Service;
using TmU220PrintHelperV2d0.Singleton;
using ZhangLogSysV1d0;

namespace PrinterControl
{
    public partial class FormPrinterSetting : Form
    {
        public FormPrinterSetting()
        {
            InitializeComponent();
            TimeOutOfTask = Properties.Settings.Default.TimeoutOfTask;
            TaskApiUrl= Properties.Settings.Default.TaskApiUrl;
            PrintTempleteApiUrl= Properties.Settings.Default.PrintTempleteApiUrl;
            textBox_TaskApiUrl.Text = TaskApiUrl;
            textBox_PrintTempleteApiUrl.Text = PrintTempleteApiUrl;

            numericUpDown_TimeOutOfTask.Value = TimeOutOfTask;
            InitFromFile();
        }
        public FormPrinterSetting(PrinterInfoSetV2d0 infoSet)
        {
            InitializeComponent();
            InfoSet = infoSet;
            InfoSet.PrinterInfoList.ForEach(x => bindingSource1.List.Add(x));
            TimeOutOfTask = Properties.Settings.Default.TimeoutOfTask;
            numericUpDown_TimeOutOfTask.Value = TimeOutOfTask;
        }
        public void InitFromFile()
        {
            try
            {
                string currDir = System.IO.Directory.GetCurrentDirectory();
                CfgFilename = currDir + @"\parameter\PrinterInfo.xml";
                if (File.Exists(CfgFilename))
                {
                    InfoSet = HotchPotchV2d0.ReadFromXmlFile<PrinterInfoSetV2d0>(CfgFilename);
                }
                InfoSet.PrinterInfoList.ForEach(x => bindingSource1.List.Add(x));
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
        }
        public string CfgFilename { get; set; }
        public PrinterInfoSetV2d0 InfoSet { get; set; }
        public List<rms_printer> PrinterInfoList = new List<rms_printer>();
        public string TaskApiUrl { get; set; }
        public  string PrintTempleteApiUrl { get; set; }
        /// <summary>
        /// 任务超时时长
        /// </summary>
        public int TimeOutOfTask { get; set; }
        /// <summary>
        /// 保存按钮，将表格数据保存为xml文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SToolStripButtonSave_Click(object sender, EventArgs e)
        {
            List<rms_printer> printerList = new List<rms_printer>();
            for( int i=0;i< bindingSource1.List.Count;i++)
            {
                printerList.Add((rms_printer)bindingSource1.List[i]);
            }
            InfoSet.PrinterInfoList = printerList;
            HotchPotchV2d0.SaveToXmFile<PrinterInfoSetV2d0>(InfoSet, CfgFilename);
            Properties.Settings.Default.TimeoutOfTask = TimeOutOfTask;
            Properties.Settings.Default.Save();            
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                InfoSet = PrinterInfoSetSingletonV2d0.Instance.GetPrinterInfoSet();
                //UpdateTableNetSingletonV1d0.Instance.Run<rms_printer>(InfoSet.PrinterInfoList);
                //string result=SetDataSingletonV1d0.Instance.Run<rms_printer>(new rms_printer());
                var printLog = new rms_order_print_log();
                printLog.act_printer = "test";
                printLog.addtime = DateTime.Now;
                printLog.printtime = DateTime.Now;
                printLog.task_uid = "aaaaaa";
                printLog.uid = 0;
                List<rms_order_print_log> logList = new List<rms_order_print_log>();
                logList.Add(new rms_order_print_log());
                //var result = UpdateTableNetSingletonV1d0.Instance.Run<rms_order_print_log>(logList);
                   
                //string result=SetDataNetSingletonV1d0.Instance.Run<rms_order_print_log>(printLog);


                if (InfoSet.PrinterInfoList != null)
                {
                    bindingSource1.List.Clear();
                    InfoSet.PrinterInfoList.ForEach(x => bindingSource1.List.Add(x));
                }
                else MessageBox.Show("远程读取超时");
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<rms_printer> printerList = new List<rms_printer>();
            for (int i = 0; i < bindingSource1.List.Count; i++)
            {
                printerList.Add((rms_printer)bindingSource1.List[i]);
            }
            InfoSet.PrinterInfoList = printerList;
            HotchPotchV2d0.SaveToXmFile<PrinterInfoSetV2d0>(InfoSet, CfgFilename);

            string taskApiUrl = textBox_TaskApiUrl.Text.Trim();
            string printTempleteApiUrl = textBox_PrintTempleteApiUrl.Text.Trim();
            Properties.Settings.Default.TimeoutOfTask = (int)numericUpDown_TimeOutOfTask.Value;
            Properties.Settings.Default.TaskApiUrl = taskApiUrl;
            Properties.Settings.Default.PrintTempleteApiUrl = printTempleteApiUrl;
            Properties.Settings.Default.Save();

            // 更新phpApiSet.xml文件
            string xmlFilename = System.IO.Directory.GetCurrentDirectory() + @"\parameter\phpApiSet.xml";
            ThinkPhpApiGroupSetV1d0 groupSet = HotchPotchV2d0.ReadFromXmlFile<ThinkPhpApiGroupSetV1d0>(xmlFilename);
            var groupFinded = groupSet.ApiGroupList.FirstOrDefault(x => x.group_name == "TaskApi");
            if(groupFinded!=null)
            {
                groupFinded.main_url = taskApiUrl;
                groupFinded.ApiList.ForEach(x => x.main_url = taskApiUrl);
            }
            groupFinded = groupSet.ApiGroupList.FirstOrDefault(x => x.group_name == "PrintTempleteApi");
            if (groupFinded != null)
            {
                groupFinded.main_url = printTempleteApiUrl;
                groupFinded.ApiList.ForEach(x => x.main_url = taskApiUrl);
            }
            HotchPotchV2d0.SaveToXmFile<ThinkPhpApiGroupSetV1d0>(groupSet, xmlFilename);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked) numericUpDown_TimeOutOfTask.Enabled = false;
            else numericUpDown_TimeOutOfTask.Enabled = true;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked) textBox_TaskApiUrl.Enabled = false;
            else textBox_TaskApiUrl.Enabled = true;
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked) textBox_PrintTempleteApiUrl.Enabled = false;
            else textBox_PrintTempleteApiUrl.Enabled = true;
        }
    }
}
